<template>
  <div class="finance">
    <a-card :bordered="false" style="border-radius:8px;margin-top:10px">
      <div class="card_title">
        财务报销
      </div>
      <div class="groupbox flex row_bet flex_wrap" style="margin-bottom: 19px">
        <a-radio-group v-model="status" style="margin-bottom:5px;" @change="statusChange">
          <a-radio-button :value="5">
            全部
          </a-radio-button>
          <a-radio-button :value="1">
            待审批
          </a-radio-button>
          <a-radio-button :value="2">
            审批未打款
          </a-radio-button>
          <a-radio-button :value="3">
            已打款
          </a-radio-button>
        </a-radio-group>
        <div>
          <a-input-search
            placeholder="商品ID / 店铺名"
            style="width: 200px;margin-right:10px;margin-bottom:5px;"
            v-model="search"
            allowClear
            @search="onSearch"
          />
          <a-button type="primary" icon="plus" @click="drawershow = true" class="br4">
            申请报销
          </a-button>
        </div>
      </div>
      <a-table
        :columns="columns"
        :rowKey="activity => activity.fin_id"
        :dataSource="activity"
        :loading="loading"
        @change="handleTableChange"
        :pagination="pagination"
      >
        <div slot="fin_goodsUrl" slot-scope="text,row">
          <div class="line-3" style="width: 288px;">{{ row.fin_goodsUrl }}</div>
        </div>
        <div slot="fin_makeUrl" slot-scope="text,row">
          <div :style="{color:row.fin_makeUrl!==null?'#39C15B':'#101010'}">{{ row.fin_makeUrl!==null?'已打款':'未打款' }}
          </div>
        </div>
        <div slot="fin_approval" slot-scope="text,row">
          <div :style="{color:row.fin_approval === '审批通过'?'#39C15B':'#101010'}">{{ row.fin_approval }}</div>
        </div>
        <div slot="active" slot-scope="row">
          <div>
            <a
              :href="'http://zsxt.letuilm.com/public/uploads/'+row.fin_fileName"
              download=""
              style="width: 60px;padding:0;">下载文档</a>
          </div>
          <a-button v-if="row.fin_makeUrl==null" type="link" @click="doedit(row)" style="width: 60px;padding:0;">修改信息
          </a-button>
          <a-button v-else type="link" @click="goaudit(row)">查看打款</a-button>
        </div>
      </a-table>
    </a-card>
    <a-drawer
      title="申请报销"
      :width="513"
      :visible="drawershow"
      :body-style="{ paddingBottom: '80px' }"
      @close="onClose"
    >
      <div class="drawer1">
        <div class="titles">报销店铺</div>
        <a-select placeholder="选择已经审核的报销店铺" v-model="form.fin_shopName" class="chooseipt" @change="titleChange">
          <a-select-option v-for="(item, index) in titleList" :key="index" :value="item">
            {{ item }}
          </a-select-option>
        </a-select>
        <div class="titles">商品链接</div>
        <a-textarea v-model="form.fin_goodsUrl" :rows="4" placeholder="填写产品链接，一行一个" class="chooseipt br4" allowClear>
        </a-textarea>
        <div class="titles">返现比例</div>
        <a-input v-model.number="form.fin_retPpt" placeholder="填写商品返现比例" class="chooseipt br4"/>
        <div class="titles">返现金额</div>
        <a-input v-model.number="form.fin_retCash" placeholder="填写商品需返现的金额" suffix="元" class="chooseipt br4"/>
        <div class="titles">商家联系方式</div>
        <a-input v-model="form.fin_contact" placeholder="商家的联系方式" class="chooseipt"/>
        <div class="titles">打款方式</div>
        <a-input v-model="form.fin_payType" placeholder="填写打款方式" class="chooseipt"/>
        <div class="titles">打款账号</div>
        <a-input v-model="form.fin_payUser" placeholder="填写打款账号" class="chooseipt"/>
        <div class="titles">详情文档</div>
        <a-upload-dragger
          name="file"
          :multiple="true"
          action="https://app.letuilm.com/api/upload"
          @change="handleChange2"
          style="width: 460px;"
        >
          <p class="ant-upload-drag-icon">
            <a-icon type="inbox"/>
          </p>
          <p class="ant-upload-text">
            点击或将文件拖拽到这里上传
          </p>
          <p class="ant-upload-hint">
            支持扩展名：.rar .zip .doc .docx .pdf .jpg...
          </p>
        </a-upload-dragger>
        <div
          :style="{
            position: 'absolute',
            right: 0,
            bottom: 0,
            width: '100%',
            borderTop: '1px solid #e9e9e9',
            padding: '10px 16px',
            background: '#fff',
            textAlign: 'right',
            zIndex: 1
          }"
        >
          <a-button
            type="primary"
            style="float:left;"
            :disabled="btnloading"
            :loading="btnloading"
            class="br4"
            @click="dosubmit">
            <a-icon v-if="btnloading" type="reload" :spin="btnloading"/>
            {{ btnloading===true?'提交中...':'提 交 申 请' }}
          </a-button>
        </div>
      </div>
    </a-drawer>
  </div>
</template>

<script>
  import { doaddfinanceinfo, doupdateinfo, getfinancelistadmin, getshopname } from '@/api/financeApi'
  import moment from 'moment'

  export default {
    name: 'Finance',
    data () {
      return {
        moment,
        drawershow: false,
        status: 5,
        search: '',
        form: {
          fin_shopName: '',
          fin_goodsUrl: '',
          fin_retPpt: '',
          fin_retCash: '',
          fin_payType: '',
          fin_contact: '',
          fin_payUser: '',
          fin_fileName: ''
        },
        titleList: [],
        columns: [
          {
            title: '提交时间',
            align: 'left',
            dataIndex: 'fin_addTime',
            scopedSlots: {
              customRender: 'fin_addTime'
            }
          },
          {
            title: '店铺名称',
            align: 'center',
            dataIndex: 'fin_shopName',
            scopedSlots: {
              customRender: 'fin_shopName'
            }
          },
          {
            title: '产品连接',
            align: 'center',
            dataIndex: 'fin_goodsUrl',
            scopedSlots: {
              customRender: 'fin_goodsUrl'
            }
          },
          {
            title: '返现比例',
            align: 'center',
            dataIndex: 'fin_retPpt',
            scopedSlots: {
              customRender: 'fin_retPpt'
            }
          },
          {
            title: '返现金额',
            align: 'center',
            dataIndex: 'fin_retCash',
            scopedSlots: {
              customRender: 'fin_retCash'
            }
          },
          {
            title: '团长账号',
            align: 'center',
            dataIndex: 'fin_user',
            scopedSlots: {
              customRender: 'fin_user'
            }
          },
          {
            title: '商家联系方式',
            align: 'center',
            dataIndex: 'fin_contact',
            scopedSlots: {
              customRender: 'fin_contact'
            }
          },
          {
            title: '打款方式',
            align: 'center',
            dataIndex: 'fin_payType',
            scopedSlots: {
              customRender: 'fin_payType'
            }
          },
          {
            title: '打款账号',
            align: 'center',
            dataIndex: 'fin_payUser',
            scopedSlots: {
              customRender: 'fin_payUser'
            }
          },
          {
            title: '税前',
            align: 'center',
            dataIndex: 'fin_pretax',
            scopedSlots: {
              customRender: 'fin_pretax'
            }
          },
          {
            title: '税后',
            align: 'center',
            dataIndex: 'fin_aftertax',
            scopedSlots: {
              customRender: 'fin_aftertax'
            }
          },
          {
            title: '备注',
            align: 'center',
            dataIndex: 'fin_reject',
            scopedSlots: {
              customRender: 'fin_reject'
            }
          },
          {
            title: '审批状态',
            align: 'center',
            // dataIndex: 'fin_approval',
            scopedSlots: {
              customRender: 'fin_approval'
            }
          },
          {
            title: '打款状态',
            align: 'center',
            dataIndex: 'fin_makeUrl',
            scopedSlots: {
              customRender: 'fin_makeUrl'
            }
          },
          {
            title: '操作',
            align: 'center',
            scopedSlots: {
              customRender: 'active'
            }
          }
        ],
        activity: [
          // {
          //   fin_id: 666, // 自增id
          //   fin_user: '从雪艳', // 用户
          //   fin_fileName: '20200821/5e6fa519db4d5628509d0a32d365511a.xlsx', // 文件名称
          //   fin_shopName: '炫恋家纺旗舰店', // 店铺名称
          //   fin_goodsUrl: 'https://detail.tmall.com/item.htm?id=617448940450', // 商品链接
          //   fin_retPpt: '50%', // 返现比例
          //   fin_retCash: '4022.00', // 返现金额
          //   fin_addTime: '2020-08-21 17:00:44', // 添加时间
          //   fin_approval: '审批通过', // 审核状态
          //   fin_makeMoney: null,
          //   fin_makeUrl: '20200824/5e2b8d28408d15b21934c644adbffa49.png', // 打款截图
          //   fin_contact: '1111', // 商家联系方式
          //   fin_payType: '支付宝', // 打款方式
          //   fin_payUser: '18905410880  肖长城', // 打款账号
          //   fin_pretax: '10', // 税前
          //   fin_aftertax: '9', // 税后
          //   fin_reject: '备注' // 审核备注
          // }
        ],
        loading: false,
        pagination: {
          pageSize: 15,
          page: 1
        },
        value: '',
        btnloading: false
      }
    },

    computed: {},
    created () {
      this.getfinancelistadmin(1, 15, 4)
      this.getshopname()
    },
    mounted () {
    },
    methods: {
      doedit (row) {
        console.log(row)
        this.form = row
        this.drawershow = true
      },
      getshopname () {
        getshopname().then(res => {
          // console.log(res)
          this.titleList = res.data
        }).catch(err => {
          console.log(err)
        })
      },
      onClose () {
        this.form = {}
        this.drawershow = false
      },
      goaudit (row) {
        this.$store.dispatch('setRowsname', row)
        this.$router.push({
          path: '/activity/myActivity/myAudit'
        })
      },
      handleChange2 (info) {
        const status = info.file.status
        // fin_fileName
        if (status !== 'uploading') {
          console.log(info.file, info.fileList)
          this.form.fin_fileName = '//' + info.file.response.data.url
        }
        if (status === 'done') {
          this.$message.success(`${info.file.name} 上传成功!`)
        } else if (status === 'error') {
          this.$message.error(`${info.file.name} 上传失败!`)
        }
      },
      dosubmit () {
        this.btnloading = true
        // console.log(this.form)
        if (!this.form.fin_id) {
          if (
            this.form.fin_shopName !== '' &&
            this.form.fin_goodsUrl !== '' &&
            this.form.fin_retPpt !== '' &&
            this.form.fin_retCash !== '' &&
            this.form.fin_payType !== '' &&
            this.form.fin_contact !== '' &&
            this.form.fin_payUser !== '' &&
            this.form.fin_fileName !== ''
          ) {
            doaddfinanceinfo(this.form).then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message.success('提交成功!')
                this.getfinancelistadmin(1, 15, 4)
                this.drawershow = false
                this.form = {}
                this.btnloading = false
              } else {
                this.btnloading = false
                this.$message.error(res.msg)
              }
            }).catch(err => {
              console.log(err)
            })
          } else {
            this.$message.info('请检查所需条件!')
          }
        } else {
          this.form.id = this.form.fin_id
          doupdateinfo(this.form).then(res => {
            // console.log(res)
            if (res.code === 200) {
              this.$message.success('修改成功!')
              this.getfinancelistadmin(1, 15, 4)
              this.drawershow = false
              this.form = {}
              this.btnloading = false
            } else {
              this.btnloading = false
              this.$message.error(res.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      },
      getfinancelistadmin (page, pagesize, status, search) {
        this.loading = true
        getfinancelistadmin({
          page: page,
          pagesize: pagesize,
          status: status, // 1未审批，2审批未打款，3已打款
          search: search
        })
          .then(res => {
            console.log(res)
            this.activity = res.data.data
            //  this.count = res.data.count
            const pagination = {
              ...this.pagination
            }
            pagination.total = res.data.count
            this.pagination = pagination
            this.loading = false
          })
          .catch(err => {
            console.log(err)
          })
      },
      handleTableChange (pagination, filters) {
        // console.log(pagination)
        const pager = {
          ...this.pagination
        }
        pager.current = pagination.current
        this.pagination = pager
        this.fetch({
          results: pagination.pageSize,
          page: pagination.current,
          ...filters
        })
      },
      fetch (params = {}) {
        // console.log('params:', params)
        this.loading = true
        this.getfinancelistadmin(params.page, 15, this.status, this.search)
      },
      onSearch (value) {
        console.log(value)
        this.getfinancelistadmin(1, 15, this.status, this.search)
      },
      statusChange (e) {
        // console.log('radio checked', e.target.value)
        this.getfinancelistadmin(1, 15, this.status, this.search)
      },
      titleChange (value) {
        // console.log(`selected ${value}`)
        this.form.fin_shopName = value
      }
    }
  }
</script>
<style lang="less">
  .drawer1 {
    .titles {
      color: rgba(16, 16, 16, 100);
      font-size: 13px;
      text-align: left;
      margin-bottom: 8px;
    }

    .chooseipt {
      width: 100%;
      margin-bottom: 25px;
    }

    /deep/ .ant-input {
      border-radius: 4px;
    }

    /deep/ .ant-select-selection {
      border-radius: 4px;
    }
  }
</style>
<style lang="less" scoped>
  .finance {
    .card_title {
      color: rgba(16, 16, 16, 100);
      font-size: 14px;
      margin-bottom: 15px;
      font-weight: 600;
    }

    .groupbox {
      /deep/ .ant-input {
        border-radius: 4px;
      }
    }

    /deep/ .ant-table-tbody {
      font-size: 13px;
    }
  }
</style>
